var levelup = require('levelup');
var db = levelup('./mydb');

export class LevelDB {

    /**
     * 读取
     * @param key 
     */
    public get(key: string) {
        return new Promise(function (resolve, reject) {
            db.get(key, function (err, value) {
                if (err) {
                    reject(err);
                }
                else {
                    resolve(value);
                } 
            });
        })
    }

    /**
     * 写入数据库
     * @param key 
     * @param value 
     */
    public set(key: string, value: object) {
        return new Promise(function (resolve, reject) {
        db.put(key, value, function (err) {
            if (err) {
                reject(err);
            }
            else {
                resolve(true);
            } 
        });
    });
    }
}